package com.zj.HJ;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;

public class HJ50 {
    public static Map<Character, Character> map = new HashMap<Character, Character>() {
        {
            put(')', '(');
            put(']', '[');
            put('}', '{');
        }
    };

    public static void main(String[] args) {
//        Scanner sc=new Scanner(System.in);
//        String num = sc.nextLine();
        System.out.println(getRes("3+2*{1+2*[-4/(8-6)+7]}"));
    }

    //3+2*{1+2*[-4/(8-6*1+3)+7]}
    public static int getRes(String str) {
        str = str.replace(" ", "");
        Stack<Character> opts = new Stack<>();
        Stack<Integer> nums = new Stack<>();
        int num = 0;
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (Character.isDigit(ch)) {
                num = ch - '0' + num * 10;
                if (i == str.length() - 1 || !Character.isDigit(str.charAt(i + 1))) {
                    nums.push(num);
                    num = 0;
                }
            } else if (ch == '(' || ch == '[' || ch == '{') {
                opts.push(ch);
            } else if (ch == ')' || ch == ']' || ch == '}') {
                while (!opts.isEmpty() && opts.peek() != (char) (map.get(ch))) {
                    caculate(opts, nums, num);
                }
                opts.pop();
            } else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
                if (ch == '-' && (i == 0 || str.charAt(i - 1) == '(' || str.charAt(i - 1) == '[' || str.charAt(i - 1) == '{')) {
                    nums.push(0);
                }
                while (!opts.isEmpty() && (((opts.peek() == '+' || opts.peek() == '-') && ch != '*' && ch != '/') || opts.peek() == '*' || opts.peek() == '/')) {
                    int res = caculate(opts, nums, num);
                }
                opts.push(ch);
            }
        }
        while (!opts.isEmpty()) {
            caculate(opts, nums, num);
        }
        return nums.pop();
    }

    public static int caculate(Stack<Character> opt, Stack<Integer> nums, int num) {
        char op = opt.pop();
        int num2 = nums.pop();
        int num1 = nums.pop();
        int res = 0;
        switch (op) {
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num1 - num2;
                break;
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num1 / num2;
                break;
        }
        nums.push(res);
        return res;
    }
}
